package com.wnxy.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wnxy.dto.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class PhoneFailureHandler implements AuthenticationFailureHandler {
    private ObjectMapper om=new ObjectMapper();
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        Result result = Result.getInstance().setStatusCode(201).setMessage(e.getMessage());
        System.out.println(result);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf8");
        response.getWriter().write(om.writeValueAsString(result));
        response.getWriter().flush();
    }
}
